package com.szbmyoubao.app.adapter

import android.view.ViewGroup
import android.widget.ImageView
import androidx.constraintlayout.utils.widget.ImageFilterView
import androidx.recyclerview.widget.RecyclerView
import com.szbmyoubao.app.bean.response.ImageItemResponse
import com.ellecity06.common.ktx.dp
import com.ellecity06.common.ktx.load
import com.youth.banner.adapter.BannerAdapter

/**
 * @author ellecity06
 * @time 2024/10/23 16:51
 * @des
 */
class ImageBannerAdapter2 @JvmOverloads constructor(
    list: List<ImageItemResponse>,
    private val click: ((Int) -> Unit)? = null
) :
    BannerAdapter<ImageItemResponse, ImageBannerAdapter2.BannerViewHolder>(
        list
    ) {

    class BannerViewHolder(var imageView: ImageView) : RecyclerView.ViewHolder(imageView)

    override fun onCreateHolder(parent: ViewGroup?, viewType: Int): BannerViewHolder {
        val imageView = ImageFilterView(parent!!.context)
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        imageView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        imageView.round = 10f.dp()
        return BannerViewHolder(imageView)
    }

    override fun onBindView(
        holder: BannerViewHolder,
        data: ImageItemResponse?,
        position: Int,
        size: Int
    ) {
        holder.imageView.load(holder.imageView.context, data?.imageUrl)
        if (click != null && data != null) {
            holder.imageView.setOnClickListener {
                click.invoke(position)
            }
        }
    }
}